/**
 * 
 */
package gui;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;

import javax.swing.JCheckBox;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.tree.TreeCellRenderer;

/**
 * @author Administrator
 * 
 */
public class CheckNodeTreeCellRenderer implements TreeCellRenderer
{

	private JCheckBox nodeRenderer = new JCheckBox();

	Color selectionBorderColor, selectionForeground, selectionBackground, textForeground,
	        textBackground;

	Object userObject;

	/**
	 * Instantiates a new check node tree cell renderer.
	 */
	public CheckNodeTreeCellRenderer()
	{
		Font fontValue;
		fontValue = UIManager.getFont("Tree.font");
		if (fontValue != null)
		{
			this.nodeRenderer.setFont(fontValue);
		}
		Boolean booleanValue = (Boolean) UIManager.get("Tree.drawsFocusBorderAroundIcon");
		this.nodeRenderer.setFocusPainted((booleanValue != null) && (booleanValue.booleanValue()));

		this.selectionBorderColor = UIManager.getColor("Tree.selectionBorderColor");
		this.selectionForeground = UIManager.getColor("Tree.selectionForeground");
		this.selectionBackground = UIManager.getColor("Tree.selectionBackground");
		this.textForeground = UIManager.getColor("Tree.textForeground");
		this.textBackground = UIManager.getColor("Tree.textBackground");
	}

	public JCheckBox getNodeRenderer()
	{
		return this.nodeRenderer;
	}

	/*
	 * (@Inheritance doc)
	 * @see javax.swing.tree.TreeCellRenderer#getTreeCellRendererComponent(javax.swing.JTree,
	 * java.lang.Object, boolean, boolean, boolean, int, boolean)
	 */
	public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected,
	        boolean expanded, boolean leaf, int row, boolean hasFocus)
	{
		String stringValue = tree.convertValueToText(value, selected, expanded, leaf, row, false);

		this.nodeRenderer.setText(stringValue);
		this.nodeRenderer.setSelected(false);

		this.nodeRenderer.setEnabled(tree.isEnabled());

		if (selected)
		{
			this.nodeRenderer.setForeground(this.selectionForeground);
			this.nodeRenderer.setBackground(this.selectionBackground);
		}
		else
		{
			this.nodeRenderer.setForeground(this.textForeground);
			this.nodeRenderer.setBackground(this.textBackground);
		}

		if ((value != null) && (value instanceof CheckNode))
		{
			Object userObject = ((CheckNode) value).getUserObject();
			this.userObject = userObject;
			this.nodeRenderer.setText(userObject.toString());
			this.nodeRenderer.setSelected(((CheckNode) value).isSelected());
		}
		return this.nodeRenderer;
	}

	public Object getUserObject()
	{
		return this.userObject;
	}
}
